$(function() {
	new Vue({
		el: '#permissionListApp',
		data: {
			dialogFormVisible:false,
			dialogGradeFormVisible:false,
			fullscreenLoading:false,
			formLabelWidth:"90px",
			tableData: [],
			editForm:{
				password:"",
				configPassword:"",
				username:"",
				grade:"",
				
			}
		},
		created:function(){
			parent.vueData.$data.hreHtml = "../permission/index.html";
			parent.vueData.activeIndex = "4";
			parent.vueData.getLeftNav("4")
			parent.vueData.$data.categoryIndex = 0
		    this.getAdminList();	
		},
		methods:{
			getAdminList(){
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.getAdminUserList,
					method: 'post',
					data: {}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						 this.tableData = response.data.result
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			
			handleDelete(row){
				if(row.grade === 0 ){
					this.$message.error('管理员账户无法删除');
				}
				else{
                    this.fullscreenLoading = true;
                    let param = {
                    	username: row.username,
                    }
                    axios({
                    	url: POSTURL.delAdminUser,
                    	method: 'post',
                    	data: param
                    }).then(response => {
                    	this.fullscreenLoading = false;
                    	if (response.data.code === 100) {
                    		this.$message({
                    			showClose: true,
                    			message: response.data.message,
                    			type: 'success'
                    		});
                    		 this.getAdminList();
                    			
                    	} else {
                    		this.$message.error(response.data.message);
                    	}
                    }).catch(error => {
                    	this.fullscreenLoading = false;
                    	console.log(error);
                    	this.$message.error('服务器出错!');
                    })
					
				}
			},
			handleEdit(row){
				this.dialogFormVisible = true;
				this.editForm.username = row.username
				this.editForm.grade = row.grade
				
			},
			configEditPassword(){
				if (stringEmpty(this.editForm.password)){
					this.$message.error('密码不能为空');
					return
				}
				if (stringEmpty(this.editForm.configPassword)){
					this.$message.error('确认密码不能为空');
					return
				}
				if (this.editForm.password != this.editForm.configPassword){
					this.$message.error('两次输入密码不一致');
					return
				}
				this.dialogFormVisible = false;
				
				this.fullscreenLoading = true;
				if(this.editForm.grade === 0){
					this.editAdminPassWord()
				}
				else{
					this.editOtherManagerPassword()
				}
			},
			editAdminPassWord(){
				let param = {
					password: this.editForm.password,
				}
				axios({
					url: POSTURL.updatePassword,
					method: 'post',
					data: param
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						this.editForm = {};
						 this.getAdminList();	
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			editOtherManagerPassword(){
			  let param = {
			  	password: this.editForm.password,
				username:  this.editForm.username,
			  }
			  axios({
			  	url: POSTURL.updateOtherUserInfo,
			  	method: 'post',
			  	data: param
			  }).then(response => {
			  	this.fullscreenLoading = false;
			  	if (response.data.code === 100) {
			  		this.$message({
			  			showClose: true,
			  			message: response.data.message,
			  			type: 'success'
			  		});
			  		this.editForm = {};
			  		 this.getAdminList();	
			  	} else {
			  		this.$message.error(response.data.message);
			  	}
			  }).catch(error => {
			  	this.fullscreenLoading = false;
			  	console.log(error);
			  	this.$message.error('服务器出错!');
			  })
		   },
		   handleEditGrade(row){
			   if(row.grade === 0 ){
			   	this.$message.error('管理员账户无法修改');
			   }
			   else{
				   console.log(row);
			      this.dialogGradeFormVisible = true;
			      this.editForm.username = row.username
			   }
			   
			   
		   },
		   configEditGrade(){
                
			  this.fullscreenLoading = true;
			     this.dialogGradeFormVisible = false;
			  let param = {
			  	username: this.editForm.username,
				grade:this.editForm.grade
			  }
			  axios({
			  	url: POSTURL.updateOtherUserGrade,
			  	method: 'post',
			  	data: param
			  }).then(response => {
			  	this.fullscreenLoading = false;
			  	if (response.data.code === 100) {
			  		this.$message({
			  			showClose: true,
			  			message: response.data.message,
			  			type: 'success'
			  		});
			  		this.editForm = {};
			  		 this.getAdminList();
			  			
			  	} else {
			  		this.$message.error(response.data.message);
			  	}
			  }).catch(error => {
			  	this.fullscreenLoading = false;
			  	console.log(error);
			  	this.$message.error('服务器出错!');
			  })
		   }
			
		}
	})
})
